<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class CodePoolController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('激活码池列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$classifyList = GiftBagService::classifyList();
		$classifyList = array('' => '所有') + $classifyList;
	
		$filter->addFilter('classify', 'select', PubFunc::t('种类'), array('options' => $classifyList, 'value' => key($classifyList)));
		$filter->addFilter('begin', 'input', '创建时间', array('value' => '', 'date' => 'day'));
		$filter->addFilter('end', 'input', '至', array('value' => '', 'date' => 'day'));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = CodePoolService::indexHeader();
		if(PubFunc::isAjax()){
			$result = CodePoolService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=codePool/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'codePool/index',
					'length'=>50,
					'page'=>$page,
					'fix' => array(
							'col' => 5
					)
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}

    
    public function add(){
    	$this->setTitle('添加激活码池');
    	
    	$classifyList = GiftBagService::classifyList();
    	$classifyList = array('' => '请选择') + $classifyList;
    	$this->out['classifyList']    = $classifyList;
    	
//     	$bagList = GiftBagService::getListGiftBag($game, 'id');
//     	$this->out['bagList'] = $bagList;
    }
    
    
    public function doAdd(){
    	$result = CodePoolService::doAdd($_POST);
    
    	if(true){
    		$this->showMessage('添加成功', PubFunc::buildUrl('/oss/?r=codePool/index'));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }
    
    public function edit(){
    	$id = $this->R('id');
    	$info = CodePoolService::get($id);
    	
    	if(empty($info)) $this->showMessage('找不到该信息');
    	
    	$this->setTitle('编辑');
    	
    	$info['start_time'] = $info['start_time'] == 0? '' : date('Y-m-d H:i',$info['start_time']);
    	$info['end_time'] = $info['end_time'] == 0? '' : date('Y-m-d H:i',$info['end_time']);
    	$this->out['info'] = $info;
    	
    	//查询渠道
    	$modCodePoolChannel = new ModCodePoolChannel();
    	$params = array();
    	$channel_list = $modCodePoolChannel->selectChannel($info['id']);
    	$this->out['channel_list']    = $channel_list;    	
    	
    	$classifyList = GiftBagService::classifyList();
    	$classifyList = array('' => '请选择') + $classifyList;
    	$this->out['classifyList']    = $classifyList;
    	
    	$backUrl = PubFunc::buildBackUrl("/oss/?r=codePool/index");  //返回路径
    	$this->out['backUrl'] = $backUrl;
    	
    	$page = $this->R('page');
    	$this->out['page'] = $page;
    }
    
    public function doEdit(){
    	$id = $this->R('id');
    	$result = CodePoolService::doEdit($id, $_POST);
    	 
    	if($result !== false){
    		$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=codePool/index"));
    	}else{
    		$this->showMessage('编辑失败');
    	}
    }
    
    public function delete(){
    	$id = $this->R('id');
    
    	$result = CodePoolService::delete($id);
    
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=codePool/index"));
    	}else{
    		$this->showMessage('删除失败',PubFunc::buildBackUrl("/oss/?r=codePool/index"));
    	}
    }
    
    /**
     * 查看激活码
     */
    public function code_view(){
    	$this->layout = '';
    	$id = PubRequest::R('id');
    	$this->out['id'] = $id; 
    	
    	$grid   = new PubGrid();
    	$filter = new PubFilter();
    	
    	$isUseList = CodePoolService::isUseList();
    	
    	$allUseList = array('' => '所有') + $isUseList;
    	$filter->addFilter('pool_id', 'hidden', '',array('value'=>$id));
    	$filter->addFilter('code_number', 'input', '激活码');
    	$filter->addFilter('is_use', 'select', PubFunc::t('状态'), array('options' => $allUseList, 'value' => key($allUseList)));
    	
    	$params = $filter->get();
    	$header = array(
        		'id'              =>array('text' => 'ID', 'width' => '50px'),
        		'code_number'     => array('text' => '激活码', 'width' => '100px'),
    			'classify'        => array('text' => '种类', 'width' => '100px'),
                'is_use'          => array('text' => '状态', 'width' => '70px'),  
        );
    	
    	$modCode = new ModCode();
    	$data = $modCode->select($params);
    	
    	$classifyList = GiftBagService::classifyList();
    	
    	foreach($data as &$v){
    		$v['is_use'] = $isUseList[$v['is_use']];
    		$v['classify'] = $classifyList[$v['classify']];
    		
    	}
    	unset($v);
    	
    	$grid->addGrid($header, $data);
    	
    	$this->out['filter'] = $filter->show();
    	$this->out['grid'] = $grid->show();
    	$this->out['fiter_special'] = 1;
    }
    
    
    public function getBag(){
    	$classify = PubRequest::R('classify');
    	$result =CodePoolService::getBag($classify);
    	$this->out = $result;
    	$this->outType = 'json';
    	
    }
    
    //导出
    public function export(){
    	$id = PubRequest::R('id');
    	$modCodePool = new ModCodePool();
    	$obj = $modCodePool->findByPk($id);
    	
    	$header = array(
    			'code_number' =>array('text' => '激活码'),  			
    	);
    	
    	$info = $obj->getAttributes();
    	
    	ini_set('memory_limit','1024M');
    	$modCode = new ModCode();
    	$params = array();
    	$params['pool_id'] = $id;
    	$codeList = $modCode->select($params);
    	
    	$now_time = date('Y-m-d H:i');
    	
    	LibExcel::exportCsv($header, $codeList, "激活码-礼包{$info['bag_id']}-{$now_time}.csv");
    }

    

}